package jd.com;


import org.apache.commons.collections.CollectionUtils;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.util.*;

/**
 * Created by wangpengzhi1 on 2018/9/11.
 */
public class MyJDialog extends JDialog {

    ArrayList<String> data1choseList = new ArrayList<String>();
    ArrayList<String> data2choseList = new ArrayList<String>();
    ArrayList<String> keyList = new ArrayList<String>();
    public MyJDialog(OperUI frame, ArrayList<Object[]> list, ArrayList<Object[]> list1, Object[] dataCol1, Object[] dataCol2){
        super(frame, "比对设置", true);
        JPanel jp = new JPanel();
        jp.setLayout(new BorderLayout());
        JButton jb_import = new JButton("按设置展示比对结果");
        jb_import.setActionCommand("getData");
        jb_import.addActionListener(new ItemChangeListener(data1choseList, data2choseList, keyList, list, list1, this, dataCol1, dataCol2));

        JPanel jp_button = new JPanel();
        jp_button.add(jb_import);
        jp.add(jp_button, BorderLayout.NORTH);

        JPanel jp_chose = new JPanel();
        jp_chose.setLayout(new GridLayout(5,1));
        JCheckBox[] jCheckBox = new JCheckBox[dataCol1.length];
        JCheckBox[] jCheckBox1 = new JCheckBox[dataCol2.length];

        JPanel jPanel_1 = new JPanel();
        JPanel jPanel_3 = new JPanel();
        double colNum = Math.ceil(dataCol1.length/7);
        jPanel_1.setLayout(new GridLayout(new Double(colNum).intValue()+1, 7));
        jPanel_3.setLayout(new GridLayout(new Double(colNum).intValue()+1, 7));
        ButtonGroup bg_1 = new ButtonGroup();
        for(int i = 0; i< dataCol1.length; i++){
            JRadioButton jRadioButton = new JRadioButton(dataCol1[i].toString());
            jRadioButton.addItemListener(new ItemChangeListener(dataCol1[i].toString(), keyList, 3));
            jPanel_1.add(jRadioButton);
            bg_1.add(jRadioButton);
            jCheckBox[i] = new JCheckBox(dataCol1[i].toString());
            jCheckBox[i].setSelected(true);
            jPanel_3.add(jCheckBox[i]);
            data1choseList.add(dataCol1[i].toString());
            ItemChangeListener listener = new ItemChangeListener(dataCol1[i].toString(), data1choseList, 1);
            jCheckBox[i].addItemListener(listener);
        }
        jPanel_1.setBorder(BorderFactory.createTitledBorder("数据集1筛选列"));
        jPanel_3.setBorder(BorderFactory.createTitledBorder("数据集1导出列"));

        JPanel jPanel_2 = new JPanel();
        JPanel jPanel_4 = new JPanel();
        double colNum1 = Math.ceil(dataCol2.length/7);
        jPanel_2.setLayout(new GridLayout(new Double(colNum1).intValue()+1, 7));
        jPanel_4.setLayout(new GridLayout(new Double(colNum1).intValue()+1, 7));
        ButtonGroup bg_2 = new ButtonGroup();
        for(int i = 0; i< dataCol2.length; i++){
            JRadioButton jRadioButton = new JRadioButton(dataCol2[i].toString());
            jRadioButton.addItemListener(new ItemChangeListener(dataCol2[i].toString(), keyList, 4));
            jPanel_2.add(jRadioButton);
            bg_2.add(jRadioButton);
            jCheckBox1[i] = new JCheckBox(dataCol2[i].toString());
            jCheckBox1[i].setSelected(true);
            jPanel_4.add(jCheckBox1[i]);
            data2choseList.add(dataCol2[i].toString());
            ItemChangeListener listener = new ItemChangeListener(dataCol2[i].toString(), data2choseList, 2);
            jCheckBox1[i].addItemListener(listener);
        }
        jPanel_2.setBorder(BorderFactory.createTitledBorder("数据集2筛选列"));
        jPanel_4.setBorder(BorderFactory.createTitledBorder("数据集2导出列"));

        JPanel jPanel_5 = new JPanel(new GridLayout(1,2));
        JCheckBox jCheckBox_in = new JCheckBox("忽略大小写");
        jCheckBox_in.setSelected(true);
        jCheckBox_in.setEnabled(false);
        jPanel_5.add(jCheckBox_in);
        jPanel_5.setBorder(BorderFactory.createTitledBorder("其他设置"));

        jp_chose.add(jPanel_1);
        jp_chose.add(jPanel_2);
        jp_chose.add(jPanel_3);
        jp_chose.add(jPanel_4);
        jp_chose.add(jPanel_5);

        jp.add(jp_chose);

        JPanel jp_all = new JPanel();
        jp_all.setLayout(new BorderLayout());
        jp_all.add(jp, BorderLayout.NORTH);

        Container container = getContentPane();
        container.add(jp_all);
        setBounds(200,200,800,650);
    }


    public MyJDialog(MyJDialog frame, ArrayList<ArrayList<Object>>  data, ArrayList<String> title){
        super(frame, "比对结果", true);
        JPanel jp = new JPanel();
        JButton jb_import = new JButton("导出比对结果");
        jb_import.setActionCommand("importExcel");
        jb_import.addActionListener(new ItemChangeListener(data, title));
        JPanel jp_button = new JPanel();
        jp_button.add(jb_import);

        JTable dataPanel = new JTable();
        dataPanel.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        JScrollPane jScrollPane1 = new JScrollPane(dataPanel);
        jScrollPane1.setAutoscrolls(true);
        jp.setLayout(new BorderLayout());
        jp.add(jp_button, BorderLayout.NORTH);
        jp.add(jScrollPane1);
        Container container = getContentPane();
        container.add(jp);
        setBounds(200,200,800,650);

        fillDataTableWithList(data, dataPanel, title);
    }

    public MyJDialog(OperUI frame, ArrayList<Object>  right, ArrayList<Object> left,  ArrayList<Object>  rightR, ArrayList<Object> leftR, int flag){
        super(frame, "校验结果", true);
        JPanel jp = new JPanel();
        JTable dataPanel = new JTable();
        JScrollPane jScrollPane1 = new JScrollPane(dataPanel);
        jScrollPane1.setAutoscrolls(true);

        JTable dataPanel1 = new JTable();
        JScrollPane jScrollPane11 = new JScrollPane(dataPanel1);
        jScrollPane11.setAutoscrolls(true);

        JTable dataPanel2 = new JTable();
        JScrollPane jScrollPanel2= new JScrollPane(dataPanel2);
        jScrollPanel2.setAutoscrolls(true);

        JTable dataPanel3 = new JTable();
        JScrollPane jScrollPanel3 = new JScrollPane(dataPanel3);
        jScrollPanel3.setAutoscrolls(true);

        jp.setLayout(new GridLayout(1,4));
        jp.add(jScrollPane1);
        jp.add(jScrollPane11);
        jp.add(jScrollPanel2);
        jp.add(jScrollPanel3);
        Container container = getContentPane();
        container.add(jp);
        setBounds(200,200,800,650);

        ArrayList<String> title = new ArrayList<String>();
        if(CollectionUtils.isNotEmpty(right)){
            title.add("左侧数据非法身份证号");
            fillDataTableWithList1(right, dataPanel, title);
        }

        ArrayList<String> titl11 = new ArrayList<String>();
        if(CollectionUtils.isNotEmpty(left)){
            titl11.add("右侧数据非法身份证号");
            fillDataTableWithList1(left, dataPanel1, titl11);
        }

        ArrayList<String> titl2 = new ArrayList<String>();
        if(CollectionUtils.isNotEmpty(leftR)){
            titl2.add("左侧数据重复身份证号或学籍号");
            fillDataTableWithList1(leftR, dataPanel2, titl2);
        }

        ArrayList<String> titl3 = new ArrayList<String>();
        if(CollectionUtils.isNotEmpty(rightR)){
            titl3.add("右侧数据重复身份证号或学籍号");
            fillDataTableWithList1(rightR, dataPanel3, titl3);
        }
    }



    public  void fillDataTableWithList(ArrayList<ArrayList<Object>> studentList,JTable jt_student, ArrayList<String> model){
        Vector vectorData = new Vector();
        for (ArrayList<Object> objArr : studentList){
            Vector vector = new Vector();
            for (int i = 0; i< objArr.size(); i++){
                vector.add(objArr.get(i));
            }
            vectorData.add(vector);
        }
        Vector vectorheader = new Vector();
        if(model != null){
            for(String name : model){
                vectorheader.add(name);
            }
        }
        DefaultTableModel defaultTableModel = new DefaultTableModel(vectorData, vectorheader);
        jt_student.setModel(defaultTableModel);
    }


    public  void fillDataTableWithList1(ArrayList<Object> studentList,JTable jt_student, ArrayList<String> model){
        Vector vectorData = new Vector();

        for (int i = 0; i< studentList.size(); i++){
            Vector vector = new Vector();
            vector.add(studentList.get(i));
            vectorData.add(vector);
        }

        Vector vectorheader = new Vector();
        if(model != null){
            for(String name : model){
                vectorheader.add(name);
            }
        }
        DefaultTableModel defaultTableModel = new DefaultTableModel(vectorData, vectorheader);
        jt_student.setModel(defaultTableModel);
    }
}
